package com.wanlong.admin.authorizaton;

import com.wanlong.utils.crypto._MD5;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;


/**
 * 自定义密码验证类，密码MD5加盐使用
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        Object tokenCredentials = encrypt(String.valueOf(usernamePasswordToken.getPassword()));
        Object userCredentials = getCredentials(info);
        return equals(tokenCredentials, userCredentials);
    }

    /**
     * 进行加密处理
     * @param password
     * @return
     */
    public String encrypt(String password){
        String pwd = _MD5.getMD5String(password);
        return pwd;
    }

}
